package com.iplant.basic.serviceImpl.dao.bms;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.bms.BMSPosition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.util.*;

/**
 * 班组管理
 *
 * @author YouWang·Peng
 * 2023-8-8 15:54:17
 */
public class BMSClassInfoDAO extends BaseDAO {
    private static BMSClassInfoDAO Instance = null;

    private static Logger logger = LoggerFactory.getLogger(BMSClassInfoDAO.class);

    private BMSClassInfoDAO() {
        super();
    }

    public static BMSClassInfoDAO getInstance() {
        if (Instance == null)
            Instance = new BMSClassInfoDAO();
        return Instance;
    }

    /**
     * 获取部门人员字典
     */
    public Map<Integer, Integer> BMS_QueryDepartmentNumMap(BMSEmployee wLoginUser, List<Integer> wDepartmentIDList,
                                                           OutResult<Integer> wErrorCode) {
        Map<Integer, Integer> wResult = new HashMap<>();
        try {
            if (wDepartmentIDList == null || wDepartmentIDList.size() <= 0) {
                return wResult;
            }

            String wSQL = StringUtils.Format("SELECT DepartmentID,count(*) FQTY FROM {0}.mbs_user where DepartmentID " +
                            "in ({1}) group by DepartmentID;",
                    DBSchemas.Basic.getDBName(), StringUtils.Join(",", wDepartmentIDList));

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wSQL = this.DMLChange(wSQL);

            List<Map<String, Object>> wQueryResult = this.queryForList(wSQL, wParamMap);

            for (Map<String, Object> wReader : wQueryResult) {
                int wDepartmentID = StringUtils.parseInt(wReader.get("DepartmentID"));
                int wFQTY = StringUtils.parseInt(wReader.get("FQTY"));
                wResult.put(wDepartmentID, wFQTY);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }
}
